<?php

/**
 * 库存控制器
 */

namespace app\admin\controller;

use think\Request;
use app\admin\service\Inventory as IventoryService;
use app\admin\validate\Inventory as IventoryValidate;

class Inventory extends BaseController
{
    /**
     * 获取商品列表
     * @param Request $request
     * @param IventoryService $IventoryService
     * @param IventoryValidate $validate
     * @return \think\response\Json
     */
    public function getList(Request $request, IventoryService $IventoryService, IventoryValidate $validate)
    {
        $checkData = $request->param();
        if (!$validate->scene('search')->check($checkData)) {
            return json(['code' => 1, 'message' => $validate->getError()]);
        }

        $param['name'] = $request->has('name') ? trim($request->param('name')) : null;
        $param['warehouse_name'] =  $request->param('warehouse_name', null);
        $param['is_futures'] =  $request->param('is_futures', null);
        $param['source_table'] =  $request->param('source_table', null);
        $param['arrival_date'] =  $request->param('arrival_date', null);
        $param['real_arrival_date'] =  $request->param('real_arrival_date', null);
        $param['if_tlz'] =  $request->param('if_tlz', null);
        $param['pageIndex'] =  $request->param('pageIndex', 1);
        $param['pageSize'] =  $request->param('pageSize', 100);
        $param['allow_zero_stock_sale'] =  $request->param('allow_zero_stock_sale', null);
        $param['allowZero'] =  $request->param('allowZero', null);
        $param['is_sale'] =  $request->param('is_sale', null);
        $param['available_qty'] =  $request->param('available_qty', null);
        $param['lock_qty'] =  $request->param('lock_qty', null);
        $param['actual_available_qty'] =  $request->param('actual_available_qty', null);
        // 新增：采购订单码搜索
        $param['purchase_no'] =  $request->param('purchase_no', null);

        $intPage =  $request->param('pageIndex', 1);
        $intPageSize =  $request->param('pageSize', 50);

        $arrData = $IventoryService->getList($param, (int)$intPage, (int)$intPageSize);
        return json($arrData);
    }

    /**
     * 获取库存数据 用于编辑
     * @param Request $request
     * @param IventoryService $IventoryService
     * @param IventoryValidate $validate
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getInfo(Request $request, IventoryService $IventoryService, IventoryValidate $validate)
    {
        $checkData = $request->param();
        if (!$validate->scene('show')->check($checkData)) {
            return json(['code' => 1, 'message' => $validate->getError()]);
        }
        $param['id'] = $request->param('id', null);
        $param['currency'] = $request->param('currency', null);
        $arrData = $IventoryService->getInfo($param);
        return json($arrData);
    }

    /**
     * 获取单条或多条数据
     * @param Request $request
     * @param IventoryValidate $validate
     * @param IventoryService $IventoryService
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function delete(Request $request, IventoryValidate $validate, IventoryService $IventoryService)
    {
        $checkData = $request->param();
        if (!$validate->scene('delete')->check($checkData)) {
            return json(['code' => 1, 'message' => $validate->getError()]);
        }
        $param['ids'] = $request->param('ids', null);
        $arrData = $IventoryService->delete($param);
        return json($arrData);
    }

    public function savePrice(Request $request, IventoryValidate $validate, IventoryService $IventoryService)
    {
        $checkData = $request->param();
        if (!$validate->scene('delete')->check($checkData)) {
            return json(['code' => 1, 'message' => $validate->getError()]);
        }
        $param['operator_id'] = $request->operator_id;
        $param['id'] = $request->param('id', null);
        $param['discount'] = $request->param('discount', []);
        $param['currency'] = $request->param('currency', null);
        $param['sales_price'] = $request->param('sales_price', null);
        $param['is_sale'] = $request->param('is_sale', null);
        $arrData = $IventoryService->savePrice($param);
        return json($arrData);
    }

    /**
     * 查询库存信息 用于编辑页
     * @param Request $request
     * @param IventoryValidate $validate
     * @param IventoryService $IventoryService
     * @return \think\response\Json
     */
    public function detail(Request $request, IventoryValidate $validate, IventoryService $IventoryService)
    {
        $checkData = $request->param();
        if (!$validate->scene('delete')->check($checkData)) {
            return json(['code' => 1, 'message' => $validate->getError()]);
        }
        $param['operator_id'] = $request->operator_id;
        $param['id'] = $request->param('id', null);
        $arrData = $IventoryService->detail($param);
        return json($arrData);
    }

    /**
     * 获取库存详情
     * @param Request $request
     * @param IventoryValidate $validate
     * @param IventoryService $IventoryService
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 2024-11-27 16 08 36
     * author: jiang
     */
    public function transDetail(Request $request, IventoryValidate $validate, IventoryService $IventoryService)
    {
        $checkData = $request->param();
        if (!$validate->scene('delete')->check($checkData)) {
            return json(['code' => 1, 'message' => $validate->getError()]);
        }
        $param['operator_id'] = $request->operator_id;
        $param['id'] = $request->param('id', null);
        //        $param['intoWarehouse'] = $request->param('intoWarehouse', null);
        $arrData = $IventoryService->transDetail($param);
        return json($arrData);
    }

    /**
     * 获取库存订单信息
     * @param Request $request
     * @param IventoryValidate $validate
     * @param IventoryService $IventoryService
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 2024-11-27 16 08 51
     * author: jiang
     */
    public function getOrderByInventory(Request $request, IventoryValidate $validate, IventoryService $IventoryService)
    {
        $checkData = $request->param();
        if (!$validate->scene('getOrderByInventory')->check($checkData)) {
            return json(['code' => 1, 'message' => $validate->getError()]);
        }
        $param['operator_id'] = $request->operator_id;
        $param['id'] = $request->param('id', null);
        $param['wid'] = $request->param('wid', null);
        $param['currentWId'] = $request->param('currentWId', null);
        $param['transferInWId'] = $request->param('transferInWId', null);
        $arrData = $IventoryService->getOrderByInventory($param);
        return json($arrData);
    }

    public function transEnum(Request $request, IventoryValidate $validate, IventoryService $IventoryService)
    {
        $checkData = $request->param();
        if (!$validate->scene('transEnum')->check($checkData)) {
            return json(['code' => 1, 'message' => $validate->getError()]);
        }
        $param['operator_id'] = $request->operator_id;
        $param['id'] = $request->param('id', null);
        $arrData = $IventoryService->transEnum($param);
        return json($arrData);
    }

    public function transSave(Request $request, IventoryValidate $validate, IventoryService $IventoryService)
    {
        $checkData = $request->param();
        if (!$validate->scene('delete')->check($checkData)) {
            return json(['code' => 1, 'message' => $validate->getError()]);
        }
        $param['operator_id'] = $request->operator_id;
        $param['id'] = $request->param('id', null);
        $param['futures'] = $request->param('futures', null);
        $param['convertible_qty'] = $request->param('convertible_qty', null);
        $param['arrival_date'] = $request->param('arrival_date', null);
        $param['list'] = $request->param('list', []);
        $param['intoWarehouse'] = $request->param('intoWarehouse', null);
        $param['arrival_batch'] = $request->param('arrival_batch', null);

        $arrData = $IventoryService->transSave($param);
        return json($arrData);
    }

    public function getFieldEnum(Request $request, IventoryService $IventoryService)
    {
        $param = $request->param('field', 'ALL');
        $arrData = $IventoryService->getFieldEnum($param);
        return json($arrData);
    }

    public function sale(Request $request, IventoryValidate $validate, IventoryService $IventoryService)
    {
        $checkData = $request->param();
        if (!$validate->scene('sale')->check($checkData)) {
            return json(['code' => 1, 'message' => $validate->getError()]);
        }
        $param['ids'] = $request->param('ids', []);
        $param['is_sale'] = $request->param('is_sale', null);
        $arrData = $IventoryService->sale($param);
        return json($arrData);
    }
    public function allowZero(Request $request, IventoryValidate $validate, IventoryService $IventoryService)
    {
        $checkData = $request->param();
        if (!$validate->scene('allowZero')->check($checkData)) {
            return json(['code' => 1, 'message' => $validate->getError()]);
        }
        $param['ids'] = $request->param('ids', []);
        $param['allow_zero_stock_sale'] = $request->param('allow_zero_stock_sale', null);
        $arrData = $IventoryService->allowZero($param);
        return json($arrData);
    }


    /**
     * 合并库存 订单选择商品
     * @param Request $request
     * @param IventoryValidate $validate
     * @param IventoryService $IventoryService
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 2024-11-19 11 53 52
     * author: jiang
     */
    public function merge(Request $request, IventoryValidate $validate, IventoryService $IventoryService)
    {
        $checkData = $request->param();
        if (!$validate->scene('merge')->check($checkData)) {
            return json(['code' => 1, 'message' => $validate->getError()]);
        }

        $param['product'] =  trim($request->param('product', null));
        $param['currency'] =  $request->param('currency', null);
        $param['wid'] =  $request->param('wid', null);
        $param['brand'] =  $request->param('brand', null);
        $param['is_futures'] =  $request->param('is_futures', null);
        $param['start_date'] =  $request->param('start_date', null);
        $param['end_date'] =  $request->param('end_date', null);
        $param['type'] =  $request->param('type', 2); // type=1(下单时获取可售 库存> 0的数据) type=2(商品报价获取库存)

        $intPage =  $request->param('pageIndex', 1);
        $intPageSize =  $request->param('pageSize', 50);

        $arrData = $IventoryService->mergeCreateOrder($param, (int)$intPage, (int)$intPageSize);
        return json($arrData);
    }

    /**
     * 获取资源
     * @param Request $request
     * @param IventoryService $IventoryService
     * @return \think\response\Json
     * 2024-11-08 13 31 59
     * author: jiang
     */
    public function resource(Request $request, IventoryService $IventoryService)
    {
        $param = $request->param('field', 'ALL');
        $arrData = $IventoryService->resource($param);
        return json($arrData);
    }

    /**
     * 导入价格
     * @param Request $request
     * @param IventoryService $IventoryService
     * @return void
     * 2024-12-03 17 48 00
     * author: jiang
     */
    public function importPrice(Request $request, IventoryService $IventoryService)
    {
        $param['operator_id'] = $request->operator_id;
        $param['currency'] = $request->param('currency', null);
        $param['file'] = $request->file('file');
        $arrData = $IventoryService->importPrice($param);
        return json($arrData);
    }

    public function exportPrice(Request $request, IventoryService $IventoryService, IventoryValidate $validate)
    {
        $checkData = $request->param();
        if (!$validate->scene('search')->check($checkData)) {
            return json(['code' => 1, 'message' => $validate->getError()]);
        }

        $param['name'] =  $request->param('name', null);
        $param['warehouse_name'] =  $request->param('warehouse_name', null);
        $param['is_futures'] =  $request->param('is_futures', null);
        $param['source_table'] =  $request->param('source_table', null);
        $param['arrival_date'] =  $request->param('arrival_date', null);

        $arrData = $IventoryService->exportPrice($param);
        return json($arrData);
    }

    /**
     * 修改价格
     * @param Request $request
     * @param IventoryService $IventoryService
     * @param IventoryValidate $validate
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 2025-01-03 17 00 15
     * author: jiang
     */
    public function fixSavePrice(Request $request, IventoryService $IventoryService, IventoryValidate $validate)
    {
        $checkData = $request->param();
        if (!$validate->scene('fixSavePrice')->check($checkData)) {
            return json(['code' => 1, 'message' => $validate->getError()]);
        }
        $param['operator_id'] = $request->operator_id;
        $param['id'] = $request->param('id', null);
        $param['product_prices'] = $request->param('product_prices', []);

        $param['is_sale'] = $request->param('is_sale', null);
        $param['allow_zero_stock_sale'] = $request->param('allow_zero_stock_sale', null);
        $param['ccname_ids'] = $request->param('ccname_ids', null);
        $param['real_arrival_date'] = $request->param('real_arrival_date', null);
        $param['remark'] = $request->param('remark', null);
        $arrData = $IventoryService->fixSavePrice($param);
        return json($arrData);
    }

    /**
     * 校对库存
     * @param Request $request
     * @param IventoryService $IventoryService
     * @param IventoryValidate $validate
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 2025-01-03 17 00 15
     * author: jiang
     */
    public function submitCheckApi(Request $request, IventoryService $IventoryService, IventoryValidate $validate)
    {
        $checkData = $request->param();
        if (!$validate->scene('submitCheckApi')->check($checkData)) {
            return json(['code' => 1, 'message' => $validate->getError()]);
        }
        $param['operator_id'] = $request->operator_id;
        $param['id'] = $request->param('id', null);
        $param['check_qty'] = $request->param('check_qty', -1);
        $param['lock_qty'] = $request->param('lock_qty', null);
        $param['available_qty'] = $request->param('available_qty', null);
        $param['action_type'] = $request->param('action_type', null);

        $param['remark'] = $request->param('remark', null);

        $arrData = $IventoryService->submitCheckApi($param);
        return json($arrData);
    }

    /**
     * 订单库存保存
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 2024-12-31 15 44 48
     * author: jiang
     */
    public function transSaveByOrder(Request $request, IventoryValidate $validate, IventoryService $service)
    {
        $checkData = $request->param();
        if (!$validate->scene('transSaveByOrder')->check($checkData)) {
            return json(['code' => 1, 'message' => $validate->getError()]);
        }
        $param['operator_id'] = $request->operator_id;
        $param['productid'] = $request->param('productid', null);
        $param['id'] = $request->param('id', null);
        $param['wid'] = $request->param('wid', null);
        $param['arrival_date'] = $request->param('arrival_date', null);
        $param['is_futures'] = $request->param('is_futures', null);
        $param['list'] = $request->param('list', []);
        $arrData = $service->transSaveByOrder($param);
        return json($arrData);
    }

    /**
     * 订单库存保存
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 2024-12-31 15 44 48
     * author: jiang
     */
    public function transSaveByWholesal(Request $request, IventoryValidate $validate, IventoryService $service)
    {
        $checkData = $request->param();

        if (!$validate->scene('transSaveByWholesal')->check($checkData)) {
            return json(['code' => 1, 'message' => $validate->getError()]);
        }
        $param['operator_id'] = $request->operator_id;
        $param['productid'] = $request->param('productid', null);
        $param['id'] = $request->param('id', null);
        $param['wid'] = $request->param('wid', null);
        $param['arrival_date'] = $request->param('arrival_date', null);
        $param['is_futures'] = $request->param('is_futures', null);
        $param['list'] = $request->param('list', []);
        $param['associated_delivery_order_ids'] = $request->param('associated_delivery_order_ids', []);
        $param['associated_delivery_order_items_ids'] = $request->param('associated_delivery_order_items_ids', []);
        $arrData = $service->transSaveByWholesal($param);
        return json($arrData);
    }

    /**
     * 提篮子库存保存
     * @param Request $request
     * @param IventoryValidate $validate
     * @param IventoryService $service
     * @return \think\response\Json
     * 2025-02-25 16 54 28
     * author: jiang
     */
    public function baskets(Request $request, IventoryValidate $validate, IventoryService $service)
    {
        $checkData = $request->param();
        if (!$validate->scene('baskets')->check($checkData)) {
            return json(['code' => 1, 'message' => $validate->getError()]);
        }
        $param['operator_id'] = $request->operator_id;
        $param['productid'] = $request->param('product_id', null);
        $param['wid'] = $request->param('wid', null);
        $param['is_futures'] = $request->param('is_futures', null);
        $param['arrival_date'] = $request->param('arrival_date', null);
        $param['is_sale'] = $request->param('is_sale', null);
        $param['available_qty'] = $request->param('available_qty', 0);
        $param['product_prices'] = $request->param('product_prices', []);
        $param['ccname_ids'] = $request->param('ccname_ids', []);
        $arrData = $service->baskets($param);
        return json($arrData);
    }

    /**
     * 库存调拨
     * @param Request $request
     * @param IventoryValidate $validate
     * @param IventoryService $service
     * @return \think\response\Json
     * 2025-02-25 16 54 28
     * author: jiang
     */
    public function transfer(Request $request, IventoryValidate $validate, IventoryService $service)
    {
        $checkData = $request->param();
        if (!$validate->scene('transfer')->check($checkData)) {
            return json(['code' => 1, 'message' => $validate->getError()]);
        }
        $param['operator_id'] = $request->operator_id;
        $param['productInfo'] = $request->param('productInfo', []);
        $param['wid'] = $request->param('wid', null);
        $param['notes'] = $request->param('notes', null);
        $param['arrival_date'] = $request->param('arrival_date', null);
        $param['ship_date'] = $request->param('ship_date', null);
        $param['cost'] = $request->param('cost', null);
        $param['transaction_currency_id'] = $request->param('transaction_currency_id', null);
        $param['logistics_no'] = $request->param('logistics_no', '');
        $param['logistics_voucher'] = $request->param('logistics_voucher', 0);
        $param['company_id'] = $request->param('company_id', 0);
        $param['cc_name_id'] = $request->param('cc_name_id', '');
        $arrData = $service->transfer($param);
        return json($arrData);
    }
    /**
     * 库存调拨列表
     * @param Request $request
     * @param IventoryValidate $validate
     * @param IventoryService $service
     * @return \think\response\Json
     * 2025-02-25 16 54 28
     * author: jiang
     */
    public function transferList(Request $request, IventoryService $service)
    {


        $param['product_id'] = $request->param('product_id', null);
        $param['old_wid'] = $request->param('old_wid', null);
        $param['new_wid'] = $request->param('new_wid', null);
        $param['start_time'] = $request->param('start_time', null);
        $param['end_time'] = $request->param('end_time', null);
        $param['logistics_no'] = $request->param('logistics_no', null);
        $param['serial_number'] = $request->param('serial_number', null);
        $param['pageIndex'] = $request->param('pageIndex', 1);
        $param['pageSize'] = $request->param('pageSize', 100);

        $arrData = $service->transferList($param);
        return json($arrData);
    }
    /**
     * 库存调拨列表
     * @param Request $request
     * @param IventoryValidate $validate
     * @param IventoryService $service
     * @return \think\response\Json
     * 2025-02-25 16 54 28
     * author: jiang
     */
    public function storage(Request $request,  IventoryService $service)
    {
        $param = $request->param();
        $param['id'] = $request->param('id', null);
        $param['arrival_batch'] = $request->param('arrival_batch', null);
        $param['operator_id'] = $request->operator_id;
        $arrData = $service->storage($param);
        return json($arrData);
    }

    /**
     * 延期入库
     * @param Request $request
     * @param IventoryValidate $validate
     * @param IventoryService $service
     * @return \think\response\Json
     * 2025-02-25 16 54 28
     * author: jiang
     */
    public function delay(Request $request,  IventoryService $service)
    {

        $param['id'] = $request->param('id', null);
        $param['real_arrival_date'] = $request->param('real_arrival_date', null);
        $param['operator_id'] = $request->operator_id;
        $arrData = $service->delay($param);
        return json($arrData);
    }
    /**
     * 库存调拨列表
     * @param Request $request
     * @param IventoryValidate $validate
     * @param IventoryService $service
     * @return \think\response\Json
     * 2025-02-25 16 54 28
     * author: jiang
     */
    public function transferDetail(Request $request,  IventoryService $service)
    {

        $id = $request->param('id', null);
        $arrData = $service->transferDetail($id);
        return json($arrData);
    }


    /**
     * 取消库存调拨
     * @param Request $request
     * @param IventoryValidate $validate
     * @param IventoryService $service
     * @return \think\response\Json
     * 2025-02-25 16 54 28
     * author: jiang
     */
    public function cancelTransfer(Request $request,  IventoryService $service)
    {

        $param['id'] = $request->param('id', null);
        $param['operator_id'] = $request->operator_id;
        $arrData = $service->cancelTransfer($param);
        return json($arrData);
    }
    /**
     * 库存调拨修改
     * @param Request $request
     * @param IventoryValidate $validate
     * @param IventoryService $service
     * @return \think\response\Json
     * 2025-02-25 16 54 28
     * author: jiang
     */
    public function transferUpdate(Request $request, IventoryValidate $validate, IventoryService $service)
    {
        $param = $request->param();

        $arrData = $service->transferUpdate($param);
        return json($arrData);
    }
}
